﻿using System;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace mozhi.smarterp.Finance.Treasuries
{
    /// <summary>
    /// 预付单和采购订单连接表
    /// </summary>
    public class PaymentOrderLink : AuditedAggregateRoot<Guid> , IMultiTenant
    {
        public Guid? TenantId { get; private set; }

        /// <summary>
        /// 采购订单
        /// </summary>
        public Guid OrderId { get; private set; }

        /// <summary>
        /// 付款单
        /// </summary>
        public Guid PaymentId { get; private set; }

        public decimal OrderAmountFr {  get; private set; }

        public decimal AdvanceAmountFr { get; private set; }

        /// <summary>
        /// 预付金额本位币
        /// </summary>
        public decimal AdvanceAmount {  get; private set; }

        private PaymentOrderLink() { }

        public PaymentOrderLink(Guid id , Guid orderId, Guid paymentId, decimal orderAmountFr, decimal advanceAmountFr)
        {
            Id = id;
            OrderId = orderId;
            PaymentId = paymentId;
            OrderAmountFr = orderAmountFr;
            AdvanceAmountFr = advanceAmountFr;

            Validate();
        }

        public void SetAdvanceAmount(decimal advanceAmount)
        {
            AdvanceAmount = advanceAmount;

            if (AdvanceAmount <= 0)
                throw new UserFriendlyException("预付金额本位币必须大于0", "100000");
        }

        #region
        private void Validate()
        {
            Check.NotDefaultOrNull<Guid>(OrderId, nameof(OrderId));
            Check.NotDefaultOrNull<Guid>(PaymentId, nameof(PaymentId));

            if (OrderAmountFr <= 0)
                throw new UserFriendlyException("订单金额必须大于0", "100000");

            if (AdvanceAmountFr <= 0)
                throw new UserFriendlyException("预付金额必须大于0", "100000");

            

        }
        #endregion
    }
}
